<?php

use app\api\model\DaoVipDetail;
use app\api\model\Userr;

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免费声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

if (!function_exists('create_sn')) {
    /**
     * 生成订单编号
     * @return string
     */
    function create_sn(): string
    {
        return date("YmdHis") . str_pad(mt_rand(1, 99999), 5, "0", STR_PAD_LEFT);
    }
}


if (!function_exists('corsOptionsHandler')) {

    /**
     * Cors Options 跨域处理
     */
    function corsOptionsHandler()
    {
        if (request()->isOptions()) {
            header('Access-Control-Allow-Origin:*');
            header('Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,token');
            header('Access-Control-Allow-Credentials:true');
            header('Access-Control-Allow-Methods:GET,POST,OPTIONS');
            header('Access-Control-Max-Age:1728000');
            header('Content-Type:text/plain charset=UTF-8');
            header('Content-Length: 0', true);
            header('status: 204');
            header('HTTP/1.0 204 No Content');
        } else {
            header('Access-Control-Allow-Origin:*');
            header('Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,token');
            header('Access-Control-Allow-Credentials:true');
            header('Access-Control-Allow-Methods:GET,POST,OPTIONS');
        }
    }
}

if (!function_exists('get_order_info')) {
    function get_order_info($order_no, $trade_no)
    {
        $config = config('pay');
        $order_info = DaoVipDetail::mk()->where(['vip_sn' => $order_no])->find();
        if (empty($order_info)) sysoplog('支付异步通知异常', '订单信息未找到，订单号：' . $order_no);
        $user_info = Userr::mk()->where(['zh' => $order_info['user_name']])->field('zh,a2,b1')->find();
        if (empty($user_info)) sysoplog('支付异步通知异常', '用户信息未找到，订单号：' . $order_no);
        if ($order_info['is_pay'] == 0) {
            #待支付
            $this_vip = [];
            foreach ($config['vip_list'] as $vip) {
                if ($vip['type'] == $order_info['type']) $this_vip = $vip;
            }
            $days = $this_vip['month'] * 30;
            if (!empty($user_info['b1']) && time() < strtotime($user_info['b1'])) {
                #会员仍在有效期
                $user_start_time = $user_info['a2'];
                $start_time = date('Y-m-d', strtotime($user_info['b1'] . ' +1 days'));
            } else {
                $user_start_time = $start_time = date('Y-m-d');
            }
            $end_time = date('Y-m-d', strtotime($start_time . " +$days days"));
            $bool = DaoVipDetail::mk()->where(['id' => $order_info['id']])->update(
                [
                    'is_pay' => 1,
                    'pay_time' => date('Y-m-d H:i:s'),
                    'start_time' => $start_time,
                    'end_time' => $end_time,
                    'transaction_id' => $trade_no
                ]
            );
            if (!$bool) sysoplog('支付状态异常', '支付状态修改失败，订单号：' . $order_no);
            Userr::mk()->where(['zh' => $order_info['user_name']])->update(
                [
                    'vip' => $order_info['type'],
                    'a2' => $user_start_time,
                    'b1' => $end_time
                ]
            );
        }
    }

}




